using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace MCGameEngine.Physic_Layer
{
    public class CollisionSphere
    {
        private BoundingSphere[] meshes_spheres;
        private BoundingSphere[] bones_spheres;

        public CollisionSphere(Model m)
        {
            AutoGenerate(m);
        }

        public void AutoGenerate(Model m)
        {
            meshes_spheres = new BoundingSphere[m.Meshes.Count];

            for (int i = 0; i < m.Meshes.Count; i++)
            {
                meshes_spheres[i] = m.Meshes[i].BoundingSphere;
            }


            bones_spheres = new BoundingSphere[m.Bones.Count];

            for (int i = 0; i < m.Bones.Count; i++)
            {
                bones_spheres[i] = new BoundingSphere();
                bones_spheres[i].Transform(m.Bones[i].Transform);

                if (i >= 1)
                {
                    bones_spheres[i].Radius = Vector3.Distance(bones_spheres[i].Center, bones_spheres[i - 1].Center);
                }
            }
        }

        public bool Collide(CollisionSphere col)
        {
            foreach (BoundingSphere bs in col.meshes_spheres)
            {
                foreach (BoundingSphere bs2 in this.meshes_spheres)
                {
                    if (bs.Intersects(bs2))
                    {
                        goto bones_check;
                    }
                }
            }
            return false;

        bones_check:

            foreach (BoundingSphere bsb in col.bones_spheres)
            {
                foreach (BoundingSphere bsb2 in this.bones_spheres)
                {
                    if (bsb.Intersects(bsb2))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}